'''requests 基本使用方式'''
import requests

'''GET 请求'''
response = requests.get(r'http://www.baidu.com')
print(type(response))
print(dir(response))
'''服务器返回的状态码 状态信息'''
print(response.status_code,response.reason)
'''cookie 信息'''
print(response.cookies)
'''text 为 获取到的响应数据'''
print(response.text)

'''requests 各种请求方式'''
# requests.post()
# requests.put()
# requests.delete()
# requests.head()
# requests.options()

'''GET 基本请求'''
url_string = r'http://httpbin.org'

'''带参数'''
url_string = url_string + '/get?uname=IG.wxz&usex=男'
response = requests.get(url_string)
print(response.text)

'''带参数的第二种写法'''
parms = {
    'uname':'王者荣耀',
    'usex':4
}
url_string = r'http://httpbin.org'
response = requests.get(url_string+'/get',params=parms)
print(response.text)

url_string = r'https://douban.uieee.com/v2/movie/in_theaters'
response = requests.get(url_string)
# print(response.text)
print(response.json())
obj = response.json()
move_list = obj['subjects']
for item in move_list:
    print(item['title'])

'''获取二进制 数据流
https://www.baidu.com/favicon.ico
'''
response = requests.get(r'https://www.baidu.com/favicon.ico')
print(response.text)
'''.content 才是 真正的字节流数据，他才是保存文件的内容
不能 .text 保存，因为 .text 只是普通的字符串数据，只用作数据解析
不能作为保存文件的数据
'''
print(response.content)

# with open('favicon.ico','wb') as f:
#     f.write(response.content)

'''添加headers'''
header = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)"
}

url_string = r'http://httpbin.org'
response = requests.get(url_string + '/headers', headers=header)
print(response.text)

'''POST'''
parms = {
    'name': 'Jack Ma',
    'work': 'CEO'
}
response = requests.post(url_string + '/post', data=parms, headers=header)
print(response.text)

'''响应response的属性 '''
response = requests.get(r'http://www.jianshu.com', headers=header)
print(response.status_code, response.reason)
print(response.headers)
print(response.cookies)
print(response.url)
print(response.history)

'''requests 的异常处理机制'''
'''异常1：非 HTTP错误（1xx～5xx）'''
response = requests.get(r'http://www.jianshu.com/404.html', headers=header)
print(response.status_code)

if not response.status_code == requests.codes.ok:
    print('404-1')

response = requests.get(r'http://www.jianshu.com', headers=header)
if response.status_code == 200:
    print('200')

'''异常2：'''
from requests.exceptions import ReadTimeout, ConnectionError, RequestException

try:
    response = requests.get(r'http://httpbin.org/get', timeout=1)
    print(response.status_code)
except ReadTimeout:
    print('Time Out')
except ConnectionError:
    print('Connection Error')
except RequestException:
    print('Request Error')

'''文件上传'''
file = {
    'file': open('favicon.ico', 'rb')
}

# response = requests.post(url_string + '/post', headers=header, files=file)
# print(response.text)

'''证书验证'''
'''无证书验证'''
# response = requests.get(r'https://www.12306.cn', headers=header)
# print(response.status_code)

'''关闭证书验证
就算关闭了 还是会警告 不会出现异常错误
'''
response = requests.get(r'https://www.12306.cn', headers=header, verify=False)
print(response.status_code)

'''消除关闭证书验证的警告'''
requests.urllib3.disable_warnings()
response = requests.get(r'https://www.12306.cn', headers=header, verify=False)
print(response.status_code)